package leading;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestNumberStr {

	public static void main(String[] args) {
		System.out.println(getCardType("012-0000001-4"));//A
		System.out.println(getCardType("adfadsfasdfasd"));//wrong
		System.out.println(getCardType("123456787"));//wrong
		System.out.println(getCardType("012-1238890#7"));//wrong
		System.out.println(getCardType("013-1234567-9"));//B
		System.out.println(getCardType("012-0000000-0"));//wrong
		System.out.println(getCardType("014-1111112-1"));//C
		System.out.println(getCardType("013-0000000-0"));//wrong
		System.out.println(getCardType("013-0000004-1"));//B
		System.out.println(getCardType("213-0000004-1"));//A
		System.out.println(getCardType("313-0000004-1"));//B
		System.out.println(getCardType("413-0000004-1"));//C
/*		System.out.println(new Date());
		String currDate = "2011-10-18";
		SimpleDateFormat sf = new SimpleDateFormat("dd MMMMM yyyy",Locale.ENGLISH);
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			//将给的字符串形式日期转成Date格式
			Date date = sf1.parse(currDate);
			//将Date格式日期转成中间是英文月的字符串
			String str = sf.format(date);
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	
	public static String getCardType(String str){
		String result = null;
		String str1 = "";
		String str2 = "";
		String str3 = "";
		if(str.length()!=13 && str.length()!=11){
			return "wrong number!";
		}
		
		if(str.contains("-")){
			String[] arrStr = str.split("-");
			if(arrStr.length != 3){
				return "wrong number!";
			}
			str1 = arrStr[0];
			str2 = arrStr[1];
			str3 = arrStr[2];
		}
		if(!str.contains("-")&&!isMatch(str3)){
			return "wrong number!";
		}
		
		if(isMatch(str)){
			str1 = str.substring(0,3);
			str2 = str.substring(3,10);
			str3 = str.substring(10);
		}
		if(str1.length()!=3||str2.length()!=7||str3.length()!=1){
			return "wrong number!";
		}
		
		if((str1.equals("012")||str1.substring(0,1).equals("2"))&&(!str2.equals("0000000"))){
			result = "A";
		}else if((str1.equals("013")||str1.substring(0,1).equals("3"))&&(!str2.equals("0000000"))){
			result = "B";
		}else if((str1.equals("014")||str1.substring(0,1).equals("4"))&&(!str2.equals("0000000"))){
			result = "C";
		}else{
			return "wrong number";
		}
		
		return result;
	}
	
	public static boolean isMatch(String str){
		Pattern tPattern = Pattern.compile("^[0-9]*$");
		Matcher tMatcher = tPattern.matcher(str);
		boolean tBoolean = tMatcher.matches();
		return tBoolean;
	}
	
}
